package com.sunjob.mroassets.server.serverImpl;

import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.domain.assets.Part;
import com.sunjob.common.domain.assets.Status;
import com.sunjob.mroassets.mapper.StatusMapper;
import com.sunjob.mroassets.server.StatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StarusServiceImpl implements StatusService {
    @Autowired
    StatusMapper statusMapper;


     @Override
    public Object getListByTypeAndStatus(String type,Integer status) {

        if ("instance".equals(type)){

            List<Instance> instances = statusMapper.getInstanceListByStatus(status);

            return instances;
        }else if ("part".equals(type)){

            List<Part> parts = statusMapper.getPartListByStatus(status);
            return parts;
        }
        return "暂无数据";
    }


    @Override
    //将实例的状态修改为故障
    public int updateInstanceRepair(Long instanceId) {
        //通过实例id调用映射层修改实例状态
        return statusMapper.updateInstanceStatus(instanceId,12);
    }

    @Override
    //将实例的状态修改为正常
    public int updateInstanceNormal(Instance instance) {
        //通过实例id调用映射层修改实例状态
        int flag = statusMapper.updateInstanceStatus(instance.getInstanceId(),11);
        for (Part part : instance.getParts()) {
            //遍历实例下的部件列表，修改维修过的部件使用寿命
            statusMapper.updateAccessoryTimeBypartId(part.getPartId());
        }
        return flag;
    }

    @Override
    //将实例的状态修改为报废
    public int updateInstanceScrap(Long instanceId) {
        //通过实例id调用映射层修改实例状态
        int flag = statusMapper.updateInstanceStatus(instanceId,13);
        return 0;
    }

    @Override
    //依次调用服务层进行处理
    public void updateEntityStatus() {
        //调用映射层维护实例数据
        statusMapper.protectInstanceStatus();
        //调用映射层维护部件数据
        statusMapper.updatePartStatus();
        //调用映射层维护零件数据
        statusMapper.updateAccessoryStatus();
    }

    @Override
    public int updateInstanceNormalByPartName(Instance instance) {
        //通过实例id调用映射层修改实例状态
        int flag = statusMapper.updateInstanceStatus(instance.getInstanceId(),11);
        for (Part part : instance.getParts()) {
            //遍历实例下的部件列表，修改维修过的部件使用寿命
            statusMapper.updateAccessoryTimeBypartName(part.getPartName(),instance.getInstanceId());
        }
        return flag;
    }
}
